home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 1.0 beta / flock-1.0RC3.en-US.win32.exe / flock / components / flockSearchAPIEbay.js < prev    next >
Text File  |  2007-10-18  |  10KB  |  255 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const EBAY_SEARCH_CID         = Components.ID('{46a6a8b0-3327-11db-a98b-0800200c9a66}');
  20.  
  21. const nsISupports                   = Components.interfaces.nsISupports;
  22. const nsIClassInfo                  = Components.interfaces.nsIClassInfo;
  23. const nsIFactory                    = Components.interfaces.nsIFactory;
  24. const nsIProperties                 = Components.interfaces.nsIProperties;
  25. const nsILocalFile                  = Components.interfaces.nsILocalFile;
  26. const nsIFile                       = Components.interfaces.nsIFile;
  27. const nsIIOService                  = Components.interfaces.nsIIOService;
  28. const nsIFileProtocolHandler        = Components.interfaces.nsIFileProtocolHandler;
  29. const nsIRDFRemoteDataSource        = Components.interfaces.nsIRDFRemoteDataSource;
  30. const nsIRDFDataSource              = Components.interfaces.nsIRDFDataSource;
  31. const flockISearchService           = Components.interfaces.flockISearchService;
  32. const nsIXMLHttpRequest             = Components.interfaces.nsIXMLHttpRequest;
  33.  
  34. const EBAY_SEARCH_CONTRACTID  = '@flock.com/?flock-search-ebay;1';
  35. const DIRECTORY_SERVICE_CONTRACTID  = '@mozilla.org/file/directory_service;1';
  36. const LOCAL_FILE_CONTRACTID         = '@mozilla.org/file/local;1';
  37. const PREFERENCES_CONTRACTID        = '@mozilla.org/preferences-service;1';
  38. const IO_SERVICE_CONTRACTID         = '@mozilla.org/network/io-service;1';
  39. const XMLHTTPREQUEST_CONTRACTID     = '@mozilla.org/xmlextras/xmlhttprequest;1'
  40.  
  41. const flockIError = Components.interfaces.flockIError;
  42. const FLOCK_ERROR_CONTRACTID = '@flock.com/error;1'
  43.  
  44. const EBAY_SEARCH_API_URL = "http://rest.api.ebay.com/restapi";
  45. const EBAY_USERNAME = "flocknflow";
  46. const REST_API_TOKEN = "jCu2LHQ247Q%3D**pWWu%2BOldh2Bj13OcHs1ZZcSytoI%3D";
  47.  
  48. // The following codes are passed to the api so that flock 
  49. // recieves commisions
  50. const EBAY_AFFILIATE_TRACKING_PARTNER_CODE = "1";
  51. const EBAY_AFFILIATE_TRACKINGID = "2500336";
  52.  
  53. //const EBAY_API_KEY = "2dd47dbb72b25cc7130a450255af779b";
  54.  
  55. function ebaySearchService() {
  56.   var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  57.   var bundle = sbs.createBundle("chrome://flock/locale/search/search.properties");
  58.   this.serviceName = bundle.GetStringFromName("flock.search.api.ebay");
  59. }
  60.  
  61. ebaySearchService.prototype.shortName = "ebay";
  62. ebaySearchService.prototype.icon = "chrome://browser/skin/flock/search/ebay.ico";
  63. ebaySearchService.prototype.ref = "urn:flock:search:ebay";
  64.  
  65. ebaySearchService.prototype.search = function (aQuery, aNumResults, aListener, aDatasource) {
  66.     var inst = this;
  67.     
  68.     if (!aQuery.length) return;
  69.     
  70.     var url = EBAY_SEARCH_API_URL + "?" 
  71.        + "CallName=GetSearchResults"
  72.        + "&RequestUserId=" + EBAY_USERNAME
  73.        + "&RequestToken=" + REST_API_TOKEN
  74.        + "&Version=491"
  75.        + "&UnifiedInput=1"
  76.        + "&Query=" + encodeURIComponent(aQuery)
  77.        + "&TrackingPartnerCode=" + EBAY_AFFILIATE_TRACKING_PARTNER_CODE
  78.        + "&TrackingID=" + EBAY_AFFILIATE_TRACKINGID;
  79.        
  80.     debug(url + " < call\n");
  81.     this.req = Components.classes[XMLHTTPREQUEST_CONTRACTID].createInstance(nsIXMLHttpRequest);
  82.     this.req instanceof Components.interfaces.nsIJSXMLHttpRequest;
  83.     this.req.open('GET', url, true);
  84.     var req = this.req;    
  85.     this.req.onreadystatechange = function (aEvt) {
  86.       if(inst.req.readyState == 4) {
  87.           //debug("\nRESPONSE\n" + inst.req.responseText);
  88.           try {
  89.               if(req.status == 200 || req.status == 201) {
  90.                  try {
  91.                      //processor(listener, inst);
  92.                      var rdf = inst.readXML(req.responseXML, aNumResults, aDatasource);
  93.                      var numResults = inst.getNumResults(req.responseXML);
  94.                      aListener.foundResults(numResults, rdf, inst.shortName, aQuery);
  95.                  } catch(e) {
  96.                      debug(e + " " + e.lineNumber+"\n");
  97.                  }
  98.               }
  99.               else {
  100.                   var faultString = req.responseText;
  101.                   // listener.onFault(faultString);
  102.               }
  103.           } catch(e) {
  104.               debug(e + " " + e.fileName + " " + e.lineNumber + "\n");
  105.               //listener.onError(inst.ERROR_PARSER);
  106.           }
  107.       }
  108.     }
  109.     
  110.     this.req.send(null);  
  111. }
  112.  
  113. ebaySearchService.prototype.getNumResults = function (xmlDoc) {
  114.     try {
  115.         var results = xmlDoc.getElementsByTagName("SearchResultItem"); 
  116.         return results.length;
  117.     } catch (ex) {
  118.     
  119.     }
  120. }
  121.  
  122. ebaySearchService.prototype.readXML = function (xmlDoc, aMaxResults, aDatasource) {
  123.   try {                                                     
  124.     var results = xmlDoc.getElementsByTagName("SearchResultItem"); 
  125.  
  126.     // Create an in-memory datasource
  127.     var rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  128.         .getService(Components.interfaces.nsIRDFService);
  129.     //var rdf = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance(Components.interfaces.nsIRDFDataSource);
  130.     var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"]
  131.         .getService(Components.interfaces.nsIRDFContainerUtils);
  132.         
  133.     var rootNode = rdfService.GetResource("urn:flock:search:ebay");
  134.     
  135.     // clear any existing results
  136.     var props=aDatasource.ArcLabelsOut(rootNode);
  137.     while(props.hasMoreElements()){
  138.         var prop=props.getNext();
  139.         var target=aDatasource.GetTarget(rootNode,prop,true);
  140.         try {
  141.           //target=target.QueryInterface(Components.interfaces.nsIRDFResource);
  142.           aDatasource.Unassert(rootNode,prop,target)
  143.         }
  144.         catch (e){
  145.             debug ('ebay error clearing the ds ' + e + '\n');
  146.         }
  147.     }        
  148.         
  149.     // Fill it with the results
  150.     rootNode = rdfService.GetResource("urn:flock:search:ebay");
  151.     var container = rdfContainerUtils.MakeSeq(aDatasource, rootNode);
  152.     
  153.     var maxResults = (results.length < aMaxResults) ? results.length : aMaxResults;
  154.     for (var i = 0; i < maxResults; i++) { 
  155.         var result = results[i];                                                                                                
  156.         var title = result.getElementsByTagName('Title')[0].firstChild.nodeValue;
  157.         var clickUrl = result.getElementsByTagName('ViewItemURL')[0].firstChild.nodeValue;
  158.         var url = result.getElementsByTagName('ViewItemURL')[0].firstChild.nodeValue;
  159.         var subject = rdfService.GetResource(url);
  160.  
  161.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#Name");
  162.         var name = rdfService.GetLiteral(title);                
  163.         aDatasource.Assert(subject, predicate, name, true);
  164.  
  165.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#URL");
  166.         var name = rdfService.GetLiteral(url);
  167.         aDatasource.Assert(subject, predicate, name, true);
  168.         
  169.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#favicon");
  170.         var name = rdfService.GetLiteral(this.icon);
  171.         aDatasource.Assert(subject, predicate, name, true);
  172.  
  173.         container.AppendElement(subject);
  174.     }
  175.  
  176.     return aDatasource;
  177.   } catch(ex) {
  178.     debug('ebaySearchService ' + ex + '\n');
  179.   }
  180. }
  181.  
  182.  
  183. ebaySearchService.prototype.flags = nsIClassInfo.SINGLETON;
  184. ebaySearchService.prototype.classDescription = "ebay Search Service";
  185. ebaySearchService.prototype.getInterfaces = function (count) {
  186.     var interfaceList = [flockISearchService, nsIClassInfo];
  187.     count.value = interfaceList.length;
  188.     return interfaceList;
  189. }
  190. ebaySearchService.prototype.getHelperForLanguage = function (count) {return null;}
  191.  
  192. // the nsISupports implementation
  193. ebaySearchService.prototype.QueryInterface =
  194. function (iid) {
  195.     if (!iid.equals(flockISearchService) && 
  196.         !iid.equals(nsIClassInfo) &&
  197.         !iid.equals(nsISupports))
  198.         throw Components.results.NS_ERROR_NO_INTERFACE;
  199.     if (iid.equals(nsIRDFDataSource) && !this.dataSourceSetup) {
  200.     }
  201.     return this;
  202. }
  203.  
  204. // Module implementation
  205. var FlockSearchModule = new Object();
  206.  
  207. FlockSearchModule.registerSelf =
  208. function (compMgr, fileSpec, location, type)
  209. {
  210.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  211.  
  212.     compMgr.registerFactoryLocation(EBAY_SEARCH_CID, 
  213.                                     "ebay Search JS Component",
  214.                                     EBAY_SEARCH_CONTRACTID, 
  215.                                     fileSpec, 
  216.                                     location,
  217.                                     type);
  218.     //necessary category registration
  219.     var catmgr = Components.classes["@mozilla.org/categorymanager;1"]
  220.         .getService (Components.interfaces.nsICategoryManager);
  221.     catmgr.addCategoryEntry('flockISearchService', 'ebay', EBAY_SEARCH_CONTRACTID, true, true);
  222. }
  223.  
  224. FlockSearchModule.getClassObject =
  225. function (compMgr, cid, iid) {
  226.     if (!cid.equals(EBAY_SEARCH_CID))
  227.         throw Components.results.NS_ERROR_NO_INTERFACE;
  228.     
  229.     if (!iid.equals(Components.interfaces.nsIFactory))
  230.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  231.     
  232.     return SeachServiceFactory;
  233. }
  234.  
  235. FlockSearchModule.canUnload =
  236. function(compMgr)
  237. {
  238.     return true;
  239. }
  240.     
  241. /* factory object */
  242. var SeachServiceFactory = new Object();
  243.  
  244. SeachServiceFactory.createInstance =
  245. function (outer, iid) {
  246.     if (outer != null)
  247.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  248.     return (new ebaySearchService()).QueryInterface(iid);
  249. }
  250.  
  251. /* entrypoint */
  252. function NSGetModule(compMgr, fileSpec) {
  253.     return FlockSearchModule;
  254. }
  255.